package com.first.nell.components.bill

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.first.nell.R
import com.first.nell.base.BaseFormAction
import com.first.nell.databinding.ViewFirstNellBillTableBinding
import com.first.nell.extend.addFirstNellBoldFirstNellStyle
import com.first.nell.extend.addFirstNellNormalFirstNellStyle

/**
<declare-styleable name="FirstNellBillTable">
<attr name="cwbtTitle" format="string" />
<attr name="cwbtDetail" format="string" />
<attr name="cwbtTitleColor" format="color" />
<attr name="cwbtDetailColor" format="color" />
<attr name="cwbtTitleSize" format="dimension" />
<attr name="cwbtDetailSize" format="dimension" />
<attr name="cwbtBottomLineHidden" format="boolean" />
<attr name="cwbtIsDetailBold" format="boolean" />
<attr name="cwbtIsTitleBold" format="boolean" />
</declare-styleable>
 * */
class TableBill : RelativeLayout, BaseFormAction {

    lateinit var mFirstNellViewFirstNellBinding: ViewFirstNellBillTableBinding
    private lateinit var vFirstNellRootFirstNellView: View

    private var cwbtTitleColor: Int = 0
    private var cwbtDetailColor: Int = 0
    private var cwbtTitle: String? = null
    private var cwbtDetail: String? = null
    private var cwbtTitleSize: Float = 0F
    private var cwbtDetailSize: Float = 0F
    private var cwbtBottomLineHidden: Boolean = true
    private var cwbtIsDetailBold: Boolean = true
    private var cwbtIsTitleBold: Boolean = true

    private var mFirstNellSupportFirstNellFragmentFirstNellManager: FragmentManager? = null

    private var mFirstNellTempFirstNellInputFirstNellValue: String = ""
    private var mFirstNellOnFirstNellActionFirstNellClickFirstNellListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array = cxt.obtainStyledAttributes(attrs, R.styleable.TableBill)
                cwbtTitleColor = array.getColor(
                    R.styleable.TableBill_cwbtTitleColor,
                    ContextCompat.getColor(cxt, R.color.black_6002)
                )
                cwbtTitle =
                    array.getString(R.styleable.TableBill_cwbtTitle)

                cwbtDetailColor = array.getColor(
                    R.styleable.TableBill_cwbtDetailColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )
                cwbtDetail =
                    array.getString(R.styleable.TableBill_cwbtDetail)

                cwbtTitleSize = array.getDimension(
                    R.styleable.TableBill_cwbtTitleSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_14
                    ).toFloat()
                )

                cwbtDetailSize = array.getDimension(
                    R.styleable.TableBill_cwbtDetailSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_14
                    ).toFloat()
                )

                cwbtBottomLineHidden =
                    array.getBoolean(R.styleable.TableBill_cwbtBottomLineHidden, true)

                cwbtIsDetailBold =
                    array.getBoolean(R.styleable.TableBill_cwbtIsDetailBold, true)

                cwbtIsTitleBold =
                    array.getBoolean(R.styleable.TableBill_cwbtIsTitleBold, true)

                array.recycle()
            }
        }
        context?.let {
            vFirstNellRootFirstNellView =
                View.inflate(it, R.layout.view_first_nell_bill_table, this)
            mFirstNellViewFirstNellBinding =
                ViewFirstNellBillTableBinding.bind(vFirstNellRootFirstNellView)
            initFirstNellSubviews()
        }
    }

    override fun initFirstNellSubviews() {
        try {
            mFirstNellViewFirstNellBinding.textBillTableTitle.text = (cwbtTitle)
            mFirstNellViewFirstNellBinding.textBillTableTitle.setTextColor(cwbtTitleColor)
            mFirstNellViewFirstNellBinding.textBillTableTitle.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwbtTitleSize
            )

            mFirstNellViewFirstNellBinding.textBillTableDetail.text = (cwbtDetail)
            mFirstNellViewFirstNellBinding.textBillTableDetail.setTextColor(cwbtDetailColor)
            mFirstNellViewFirstNellBinding.textBillTableDetail.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwbtDetailSize
            )

            if (cwbtBottomLineHidden) {
                mFirstNellViewFirstNellBinding.rlBillTableBottomLine.visibility = GONE
            } else {
                mFirstNellViewFirstNellBinding.rlBillTableBottomLine.visibility = VISIBLE
            }

            if (cwbtIsTitleBold) {
                mFirstNellViewFirstNellBinding.textBillTableTitle.addFirstNellBoldFirstNellStyle()
            } else {
                mFirstNellViewFirstNellBinding.textBillTableTitle.addFirstNellNormalFirstNellStyle()
            }

            if (cwbtIsDetailBold) {
                mFirstNellViewFirstNellBinding.textBillTableDetail.addFirstNellBoldFirstNellStyle()
            } else {
                mFirstNellViewFirstNellBinding.textBillTableDetail.addFirstNellNormalFirstNellStyle()
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun setFirstNellContent(content: String) {
        cwbtDetail = content
        mFirstNellViewFirstNellBinding.textBillTableDetail.text = (cwbtDetail)
    }

    override fun setFirstNellHint(hint: String) {

    }

    override fun setFirstNellTitle(title: String) {
        cwbtTitle = title
        mFirstNellViewFirstNellBinding.textBillTableTitle.text = (cwbtTitle)
    }

    override fun setFirstNellDetail(detail: String) {
        cwbtDetail = detail
        mFirstNellViewFirstNellBinding.textBillTableDetail.text = (cwbtDetail)

        //对货币、天数做一个灰色处理
//        val currency = TranslateUtil.getFirstNellCountryFirstNellCurrency()
//        mFirstNellViewFirstNellBinding.textBillTableDetail.addFirstNellTipsFirstNellStyle(
//            currency,
//            0.8F,
//            ContextCompat.getColor(context, R.color.black_text_detail)
//        )
//
//        val perDay = TranslateUtil.getGarlicPerCountryFirstNellDay()
//        mFirstNellViewFirstNellBinding.textBillTableDetail.addFirstNellTipsFirstNellStyle(
//            perDay,
//            0.8F,
//            ContextCompat.getColor(context, R.color.black_text_detail)
//        )
    }

    override fun setFirstNellDetailFirstNellColor(detailColor: Int) {
        cwbtDetailColor = detailColor
        mFirstNellViewFirstNellBinding.textBillTableDetail.setTextColor(cwbtDetailColor)
    }

    override fun setFirstNellTitleFirstNellColor(titleColor: Int) {
        cwbtTitleColor = titleColor
        mFirstNellViewFirstNellBinding.textBillTableTitle.setTextColor(cwbtTitleColor)
    }

    override fun setFirstNellFragmentFirstNellManager(manager: FragmentManager) {
        mFirstNellSupportFirstNellFragmentFirstNellManager = manager
    }

    override fun setFirstNellOnFirstNellActionFirstNellClickFirstNellListener(listener: (Int) -> Unit) {
        mFirstNellOnFirstNellActionFirstNellClickFirstNellListener = listener
    }
}